package servlet;

import com.alibaba.fastjson.JSON;

import pojo.wenxian;
import pojo.wenxian1;
import service.impl.wenxianSerice;
import service.impl.wenxianimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet("/wj/*")
public class wj extends servlet{
    private wenxianSerice xiewenServices= new wenxianimpl();

    public void file(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        du du=new du();int total=0;
        List<du> dus = du.du1();
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize+begin; List<du> list=new ArrayList<>();
       PageBean<du> duPageBean = new PageBean<du>();
       for(du he:dus)
       {
           total++;

       }

        if(size>total)
        {
            size=total;
        }
        for(int i=begin;i<size;i++)
        {
            list.add(dus.get(i));
        }
        duPageBean.setRows(list);
       duPageBean.setTotalCount(total);
        String jsonString = JSON.toJSONString(duPageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void shanwen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        String a = "D:\\wenxian-master\\wenxian-master\\src\\main\\webapp\\upload\\" + params;
        System.out.println(a);
        File file = new File(a); // 替换为要删除的文件的路径
        file.delete();
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write("success");
    }
    public void shan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        String a = "D:\\wenxian-master\\wenxian-master\\src\\main\\webapp\\upload\\" + params;
        System.out.println(a);
        File file = new File(a); // 替换为要删除的文件的路径
        file.delete();
        xiewenServices.shan(params);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write("success");
    }
    public void xiu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        wenxian1 wenxian1 = JSON.parseObject(params, wenxian1.class);
        xiewenServices.xiu(wenxian1);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write("success");
    }
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        String kai = request.getParameter("kai");
        String jie = request.getParameter("jie");
        if(kai!=null&&kai.length() > 0)
        {
            Date date = Date.valueOf(kai);
            Date date1 = Date.valueOf(jie);
           PageBean<wenxian1> shijian = xiewenServices.shijian(currentPage, pageSize, date, date1);
            String jsonString = JSON.toJSONString(shijian);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
        else{ BufferedReader br = request.getReader();
            String params = br.readLine();//json字符串
            wenxian1 wenxian1 = JSON.parseObject(params, wenxian1.class);
            PageBean<wenxian1> pageBean = xiewenServices.selectByPageAndCondition(currentPage,pageSize,wenxian1);
            String jsonString = JSON.toJSONString(pageBean);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);}
        // 获取查询条件对象

    }
    public String highlightText(String text, String keyword) {
        String highlight = "<span style=\"color:red\">" + keyword + "</span>";
        int index = text.indexOf(keyword);
        int start = Math.max(index - 150, 0);
        int end = Math.min(index + 150 + keyword.length(), text.length());
        String pre = text.substring(start, index).replaceAll(keyword, highlight);
        String post = text.substring(index + keyword.length(), end).replaceAll(keyword, highlight);
        return pre + highlight + post;
    }
    public void up(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
//        BufferedReader br = request.getReader();
//        String filename = br.readLine();//json字符串
        //获取文件名
        String filename = request.getParameter("filename");
        //文件所在的文件夹
        String folder="/upload/";
        //通知浏览器以下载的方式打开
        response.addHeader("Content-Type","application/octet-stream");
//        String a;a=new String(filename.getBytes("iso8859-1"),"utf-8");
        response.setCharacterEncoding("utf-8");
        response.addHeader("Content-Disposition","attachment;filename="+new String(filename.getBytes("utf-8"), "ISO-8859-1"));
        System.out.println(filename);
        //通过文件输入流读取文件
        InputStream in=getServletContext().getResourceAsStream(folder+ filename);
        OutputStream out=response.getOutputStream();
        byte[] bytes=new byte[1024];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
    }

}
